package com.magicborrow.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.magicborrow.BaseLog;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;

/**
 * Created by River on 6/28/16.
 */
public class UmengUtils {

    private static final String FILE_NAME = "umeng_stuff";
    private static final String UMENG_TOKEN = "umeng_token";

    public static void startPush(final Context context){
        // 开启推送
        PushAgent mPushAgent = PushAgent.getInstance(context);
        mPushAgent.enable(new IUmengRegisterCallback() {
            @Override
            public void onRegistered(String s) {
                String device_token = UmengRegistrar.getRegistrationId(context);
                UmengUtils.saveDeviceToke(context,device_token);
                BaseLog.e("==device_token==",device_token);
            }
        });
    }

    public static void saveDeviceToke(Context context,String token){
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(UMENG_TOKEN, token);
        editor.commit();
    }

    public static String getToken(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getString(UMENG_TOKEN, "");
    }

}
